package cn.xiaoxiang.org.controller;

import cn.xiaoxiang.basic.util.AjaxResult;
import cn.xiaoxiang.basic.util.PageList;
import cn.xiaoxiang.org.domain.Type;
import cn.xiaoxiang.org.query.TypeQuery;
import cn.xiaoxiang.org.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ty")
public class TypeController {

    @Autowired
    private TypeService typeService;
    @GetMapping()
    public List<Type> findAll(){
        return typeService.findAll();
    }

    @GetMapping("/{id}")
    public Type findById( @PathVariable("id") Long id) {
        return typeService.findById(id);

    }

    //刪除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        typeService.delete(id);
        return AjaxResult.me().success();
    }

    //批量刪除
    @PatchMapping
    public AjaxResult PiDelete(@RequestBody List<Long> ids) {
        typeService.batchdelete(ids);
        return AjaxResult.me().success();
    }
//添加或者修改
@PutMapping()
public AjaxResult addOrUpdate(@RequestBody Type type) {
    if (null != type.getId()) {
        // 修改
        typeService.update(type);
    } else {
        // 添加
        typeService.add(type);
    }
    return AjaxResult.me().success();
}
    //分頁
    @PostMapping()
    public PageList<Type> queryByPage(@RequestBody TypeQuery query) {
        return typeService.queryByPage(query);
    }

}
